package com.uetoken.cn.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.uetoken.cn.R;
import com.uetoken.cn.activity.TransferAccountsActivity;
import com.uetoken.cn.adapter.holder.UserTransferHolder;
import com.uetoken.cn.adapter.model.AddTransferUserModel;
import com.uetoken.cn.adapter.model.FundAssetDetailsModel;
import com.uetoken.cn.common.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddTransferUserAdapter extends RecyclerView.Adapter<AddTransferUserAdapter.MyViewHolder> {

    private Context mContext;
    //    private List<View> mViewList;
    private List<AddTransferUserModel> mAddTransferUserModelList;
    private boolean isAddData = true;
    private OnIsBatchTransferClickListener mOnIsBatchTransferClickListener;
    private OnItemClickListener mOnItemClickListener;
    private String status = "";
    private String str1 = "";
    private String str2 = "";

    public interface OnIsBatchTransferClickListener {
        void onIsBatchTransferClick(AddTransferUserModel mAddTransferUserModel, int position);
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public void setOnIsBatchTransferClickListener(OnIsBatchTransferClickListener onIsBatchTransferClickListener) {
        this.mOnIsBatchTransferClickListener = onIsBatchTransferClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public AddTransferUserAdapter(Context context, List<AddTransferUserModel> list) {
        this.mContext = context;
        this.mAddTransferUserModelList = list;
//        this.mViewList = viewList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_transfer_user, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
//        view.setTag(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        final AddTransferUserModel mAddTransferUserModel = mAddTransferUserModelList.get(position);
        mAddTransferUserModel.setId(position);
        String mAccount = mAddTransferUserModel.getAccount();
        String mFund = mAddTransferUserModel.getFund();
        holder.mEnterAccountEt.setText(mAccount == null ? "" : mAccount);
        holder.mEnterMoneyEt.setText(mFund == null ? "" : mFund);
        if (mAddTransferUserModel.getStatus() == 0) {
            holder.mTransferStatusTv.setVisibility(View.GONE);
        } else {
            holder.mTransferStatusTv.setVisibility(View.VISIBLE);
            if (mAddTransferUserModel.getStatus() == Constant.BATCH_TRANSFER_SUCCEED) {//成功
                holder.mTransferStatusTv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.icon_succeed_right), null, null, null);
                holder.mTransferStatusTv.setText(mContext.getResources().getString(R.string.str_transfer_succeed));
                holder.mTransferStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_color_theme_4));
            } else if (mAddTransferUserModel.getStatus() == Constant.BATCH_TRANSFER_FAIL) {//失败
                holder.mTransferStatusTv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.icon_failure_wrong), null, null, null);
                holder.mTransferStatusTv.setText(mContext.getResources().getString(R.string.str_transfer_fail));
                holder.mTransferStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_color_theme_1));
            } else {
                holder.mTransferStatusTv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                holder.mTransferStatusTv.setText("-");
                holder.mTransferStatusTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_color_common_black));

            }
        }

//        judgeStatus(5);

        holder.mEnterAccountEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.mEnterMoneyEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.mTransferStatusTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });

        holder.mDelUserTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mEnterAccountEt.setText("");
                holder.mEnterMoneyEt.setText("");
                mOnIsBatchTransferClickListener.onIsBatchTransferClick(mAddTransferUserModel, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mAddTransferUserModelList == null ? 0 : mAddTransferUserModelList.size();
    }

    public List<AddTransferUserModel> getList() {
        return mAddTransferUserModelList;
    }

//    public boolean checkData() {
//        status = "";
//        str1 = "";
//        str2 = "";
//        if (mAddTransferUserModelList.size() > 0) {
//            for (AddTransferUserModel model : mAddTransferUserModelList) {
//                String account = model.getAccount();
//                String fund = model.getFund();
//                LogUtils.dTag("getBatchTransferData==1", "getBatchTransferData== account = " + account + " fund = " + fund);
//
//                if (account == null || "".equals(account) || "null".equals(account)
//                        || fund == null || "".equals(fund) || "null".equals(fund)) {
//                    status = "";
//                    str1 = "";
//                    str2 = "";
//                    if (fund == null || "".equals(fund) || "null".equals(fund)) {
//                        str2 = mContext.getResources().getString(R.string.str_transfer_in_money_hint);
//                    }
//                    if (account == null || "".equals(account) || "null".equals(account)) {
//                        str1 = mContext.getResources().getString(R.string.str_transfer_in_account_hint);
//                    }
//                    status = !"".equals(str1) && !"".equals(str2) ? "1、" + str1 + ";\n2、" + str2 + ";" : str1 + str2;
//                    return false;
//                } else {
//                    status = "";
//                    str1 = "";
//                    str2 = "";
//                    double number = Double.parseDouble(fund);
//                    if (number <= 0) {
//                        status = mContext.getResources().getString(R.string.str_transfer_in_money_hint);
//                        return false;
//                    }
//                }
//            }
//        } else {
//            return false;
//        }
//        return true;
//    }

    // 添加数据
    public void addData(int position) {
        isAddData = false;
        mAddTransferUserModelList.add(position, new AddTransferUserModel());
        notifyItemInserted(position);
        if (position != mAddTransferUserModelList.size()) {
            notifyItemRangeChanged(position, mAddTransferUserModelList.size() - position);
        }
    }

    // 删除数据
    public void removeData(int position) {
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mAddTransferUserModelList.size());
    }

    //更新数据
    public void updateIitem(int position) {
        notifyItemChanged(position);
    }

    // 删除数据
    public void removeAllData() {
        mAddTransferUserModelList.clear();
        notifyDataSetChanged();
    }

    public boolean isAddData() {
        return isAddData;
    }

//    public void judgeStatus(int type) {
//        mOnIsBatchTransferClickListener.onIsBatchTransferClick(status, isAddData, type);
//        isAddData = checkData();
//        if (mOnIsBatchTransferClickListener != null) {
//            mOnIsBatchTransferClickListener.onIsBatchTransferClick(status, isAddData, type);
//        }
//        if (status == null || "".equals(status) || "null".equals(status)) {
//            isAddData = true;
//        }
//    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.enterAccountEt)
        EditText mEnterAccountEt;
        @BindView(R.id.enterMoneyEt)
        EditText mEnterMoneyEt;
        @BindView(R.id.transferStatusTv)
        TextView mTransferStatusTv;
        @BindView(R.id.delUserTv)
        TextView mDelUserTv;

        //因为删除有可能会删除中间条目，然后会造成角标越界，所以必须整体刷新一下！
        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
//            mEnterAccountEt = (EditText) view.findViewById(R.id.enterAccountEt);
//            mEnterMoneyEt = (EditText) view.findViewById(R.id.enterMoneyEt);
//            mDelUserTv = (TextView) view.findViewById(R.id.delUserTv);
        }
    }
}
